package cate.game.role.bag.prop.continuepack;

import cate.common.table.item.ContinuePackRow;
import cate.game.GameBody;
import cate.game.res.MixRes;
import cate.game.role.Role;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import org.springframework.data.annotation.Transient;

@NoteClass("连开礼包")
public class ContinuePack {
    @NoteField(value = "礼包tid,新获得时tid=0")
    public int tid;

    @NoteField(value = "下一级tid")
    public int nextTid;

    @NoteField(value = "物品tid")
    public int itemTid;

    @NoteField(value = "礼包获取时间")
    public long gainTime;

    @NoteField(value = "礼包上次打开时间")
    public long openTime;

    @NoteField(value = "今天是否可以开启")
    @JsonIgnore
    public boolean todayEnable = true;

    public EcResult<Void> openCheck(Role role){
        EcResult<Void> r = new EcResult<>();
        if(!todayEnable){
            return r.fail("今天不能再开拉");
        }
        ContinuePackRow packRow = getRow(role.getGame());
        if(packRow == null){
            return r.fail("礼包不可打开");
        }
        return r;
    }

    @JsonIgnore
    @Transient
    private ContinuePackRow getRow(GameBody gameBody){
        return gameBody.table.item.continuePack.get(nextTid);
    }

    @JsonIgnore
    @Transient
    private ContinuePackRow getNextRow(GameBody gameBody){
        return gameBody.table.item.continuePack.findNextPack(tid);
    }

    private EcResult<Void> limitCheck(Role role, ContinuePackRow packRow){
        EcResult<Void> r = new EcResult<>();
        if(packRow.limitType == 1 && role.getBase().level < packRow.limitValue){
            return r.fail("等到达到" + packRow.limitValue + "后可开启礼包");
        } else if(packRow.limitType == 2 && role.getGame().getOpenDay() < packRow.limitValue) {
            return r.fail("开服" + packRow.limitValue + "天后可开启礼包");
        } else if (packRow.limitType == 3 && role.getBase().vip < packRow.limitValue) {
            return r.fail("VIP等级达到" + packRow.limitValue + "后可开启礼包");
        }
        return r;
    }

    public EcResult<Void> open(Role role, int selectedIndex, MixRes totalReward){
        EcResult<Void> r = new EcResult<>();
        ContinuePackRow row = getRow(role.getGame());
        if(row == null){
            return r.fail("礼包开完拉");
        }
        r = limitCheck(role, row);
        if(!r.ok()){
            return r;
        }
        //会占用内存，先实现3
        MixRes reward = null;
        if(row.openType == 3){
            reward = new MixRes(row.rewardStr);
        }
        if(reward != null){
            totalReward.addList(reward);
        }
        tid = row.id;
        ContinuePackRow nextRow = getNextRow(role.getGame());
        if(nextRow == null){
            nextTid = 0;
        }else{
            nextTid = nextRow.id;
        }
        openTime = System.currentTimeMillis();
        return r;
    }

    public boolean over(){
        return nextTid == 0;
    }
}
